﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYRevitCode.FormUtility.ModelessFormControlSuite.ViewLayer;

namespace AdjustInstallationHeight
{
	public partial class OverallForm : BaseNoneSwitchForm
	{
		public List<BuiltInCategory> LstBuiltInCategory
		{
			get
			{
				return this.m_lstBuiltInCategory;
			}
			private set
			{
				this.m_lstBuiltInCategory = value;
			}
		}

		public List<FormStructure> LstFormData
		{
			get
			{
				return this.m_lstFormData;
			}
			set
			{
				this.m_lstFormData = value;
			}
		}

		public OverallForm(UIDocument inputDoc, IWin32Window input) : base(inputDoc, input)
		{
			this.InitializeComponent();
		}

		private void txtCableToBeam_TextChanged(object sender, EventArgs e)
		{
			if (this.chkCableTray.Checked && !GroupCalculation.JudgeIsNaturalNumber(this.txtCableToBeam.Text))
			{
				YJKMessageBox.Show("桥架距离梁底距离取值范围为自然数");
				this.txtCableToBeam.Text = "300";
				this.txtCableToBeam.Focus();
				this.txtCableToBeam.SelectAll();
			}
			this.RefreshFormData();
		}

		private void txtCableToFloor_TextChanged(object sender, EventArgs e)
		{
			if (this.chkCableTray.Checked && !GroupCalculation.JudgeIsNaturalNumber(this.txtCableToFloor.Text))
			{
				YJKMessageBox.Show("桥架距离板底距离取值范围为自然数");
				this.txtCableToFloor.Text = "300";
				this.txtCableToFloor.Focus();
				this.txtCableToFloor.SelectAll();
			}
			this.RefreshFormData();
		}

		private void txtDuctToBeam_TextChanged(object sender, EventArgs e)
		{
			if (this.chkDuct.Checked && !GroupCalculation.JudgeIsNaturalNumber(this.txtDuctToBeam.Text))
			{
				YJKMessageBox.Show("风管距离梁底距离取值范围为自然数");
				this.txtDuctToBeam.Text = "100";
				this.txtDuctToBeam.Focus();
				this.txtDuctToBeam.SelectAll();
			}
			this.RefreshFormData();
		}

		private void txtDuctToFloor_TextChanged(object sender, EventArgs e)
		{
			if (this.chkDuct.Checked && !GroupCalculation.JudgeIsNaturalNumber(this.txtDuctToFloor.Text))
			{
				YJKMessageBox.Show("风管距离板底距离取值范围为自然数");
				this.txtDuctToFloor.Text = "100";
				this.txtDuctToFloor.Focus();
				this.txtDuctToFloor.SelectAll();
			}
			this.RefreshFormData();
		}

		private void txtPipeToBeam_TextChanged(object sender, EventArgs e)
		{
			if (this.chkPipe.Checked && !GroupCalculation.JudgeIsNaturalNumber(this.txtPipeToBeam.Text))
			{
				YJKMessageBox.Show("水管距离梁底距离取值范围为自然数");
				this.txtPipeToBeam.Text = "100";
				this.txtPipeToBeam.Focus();
				this.txtPipeToBeam.SelectAll();
			}
			this.RefreshFormData();
		}

		private void txtPipeToFloor_TextChanged(object sender, EventArgs e)
		{
			if (this.chkPipe.Checked && !GroupCalculation.JudgeIsNaturalNumber(this.txtPipeToFloor.Text))
			{
				YJKMessageBox.Show("水管距离板底距离取值范围为自然数");
				this.txtPipeToFloor.Text = "350";
				this.txtPipeToFloor.Focus();
				this.txtPipeToFloor.SelectAll();
			}
			this.RefreshFormData();
		}

		private void chkCableTray_CheckedChanged(object sender, EventArgs e)
		{
			this.txtCableToBeam.Enabled = this.chkCableTray.Checked;
			this.txtCableToFloor.Enabled = this.chkCableTray.Checked;
			this.RefreshFormData();
			base.SendContinueMessage();
		}

		private void chkDuct_CheckedChanged(object sender, EventArgs e)
		{
			this.txtDuctToBeam.Enabled = this.chkDuct.Checked;
			this.txtDuctToFloor.Enabled = this.chkDuct.Checked;
			this.RefreshFormData();
			base.SendContinueMessage();
		}

		private void chkPipe_CheckedChanged(object sender, EventArgs e)
		{
			this.txtPipeToBeam.Enabled = this.chkPipe.Checked;
			this.txtPipeToFloor.Enabled = this.chkPipe.Checked;
			this.RefreshFormData();
			base.SendContinueMessage();
		}

		private void RefreshFormData()
		{
			this.LstFormData.Clear();
			if (this.chkCableTray.Checked && this.txtCableToBeam.Text != string.Empty && this.txtCableToFloor.Text != string.Empty)
			{
				double dDistanceToBeam = Convert.ToDouble(this.txtCableToBeam.Text);
				double dDistanceToFloor = Convert.ToDouble(this.txtCableToFloor.Text);
				this.LstFormData.Add(new FormStructure(dDistanceToBeam, dDistanceToFloor, BuiltInCategory.OST_CableTray));
			}
			if (this.chkDuct.Checked && this.txtDuctToBeam.Text != string.Empty && this.txtDuctToFloor.Text != string.Empty)
			{
				double dDistanceToBeam = Convert.ToDouble(this.txtDuctToBeam.Text);
				double dDistanceToFloor = Convert.ToDouble(this.txtDuctToFloor.Text);
				this.LstFormData.Add(new FormStructure(dDistanceToBeam, dDistanceToFloor, BuiltInCategory.OST_DuctCurves));
			}
			if (this.chkPipe.Checked && this.txtPipeToBeam.Text != string.Empty && this.txtPipeToFloor.Text != string.Empty)
			{
				double dDistanceToBeam = Convert.ToDouble(this.txtPipeToBeam.Text);
				double dDistanceToFloor = Convert.ToDouble(this.txtPipeToFloor.Text);
				this.LstFormData.Add(new FormStructure(dDistanceToBeam, dDistanceToFloor, BuiltInCategory.OST_PipeCurves));
			}
			this.GetLstBuiltInCategory(this.LstFormData);
		}

		private void GetLstBuiltInCategory(List<FormStructure> inputlstFormData)
		{
			this.m_lstBuiltInCategory.Clear();
			foreach (FormStructure formStructure in inputlstFormData)
			{
				this.m_lstBuiltInCategory.Add(formStructure.ElementType);
			}
		}

		private void OverallForm_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			if (!this.ReadDlgData())
			{
				this.chkCableTray.Checked = true;
				this.chkDuct.Checked = true;
				this.chkPipe.Checked = true;
				this.txtCableToBeam.Text = "300";
				this.txtCableToFloor.Text = "300";
				this.txtDuctToBeam.Text = "100";
				this.txtDuctToFloor.Text = "100";
				this.txtPipeToBeam.Text = "100";
				this.txtPipeToFloor.Text = "350";
			}
			this.RefreshFormData();
			base.SendContinueMessage();
		}

		private void SaveDlgData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "AdjustInstallationHeightOverall.xml", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.chkCableTray.Checked);
			streamWriter.WriteLine(this.txtCableToBeam.Text);
			streamWriter.WriteLine(this.txtCableToFloor.Text);
			streamWriter.WriteLine(this.chkDuct.Checked);
			streamWriter.WriteLine(this.txtDuctToBeam.Text);
			streamWriter.WriteLine(this.txtDuctToFloor.Text);
			streamWriter.WriteLine(this.chkPipe.Checked);
			streamWriter.WriteLine(this.txtPipeToBeam.Text);
			streamWriter.WriteLine(this.txtPipeToFloor.Text);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private bool ReadDlgData()
		{
			string path = Path.GetTempPath() + "AdjustInstallationHeightOverall.xml";
			if (!File.Exists(path))
			{
				return false;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			if (streamReader.ReadLine().ToBool(false))
			{
				this.chkCableTray.Checked = true;
				this.txtCableToBeam.Text = streamReader.ReadLine();
				this.txtCableToFloor.Text = streamReader.ReadLine();
			}
			else
			{
				this.chkCableTray.Checked = false;
				this.txtCableToBeam.Enabled = false;
				this.txtCableToFloor.Enabled = false;
				this.txtCableToBeam.Text = streamReader.ReadLine();
				this.txtCableToFloor.Text = streamReader.ReadLine();
			}
			if (streamReader.ReadLine().ToBool(false))
			{
				this.chkDuct.Checked = true;
				this.txtDuctToBeam.Text = streamReader.ReadLine();
				this.txtDuctToFloor.Text = streamReader.ReadLine();
			}
			else
			{
				this.chkDuct.Checked = false;
				this.txtDuctToBeam.Enabled = false;
				this.txtDuctToFloor.Enabled = false;
				this.txtDuctToBeam.Text = streamReader.ReadLine();
				this.txtDuctToFloor.Text = streamReader.ReadLine();
			}
			if (streamReader.ReadLine().ToBool(false))
			{
				this.chkPipe.Checked = true;
				this.txtPipeToBeam.Text = streamReader.ReadLine();
				this.txtPipeToFloor.Text = streamReader.ReadLine();
			}
			else
			{
				this.chkPipe.Checked = false;
				this.txtPipeToBeam.Enabled = false;
				this.txtPipeToFloor.Enabled = false;
				this.txtPipeToBeam.Text = streamReader.ReadLine();
				this.txtPipeToFloor.Text = streamReader.ReadLine();
			}
			streamReader.Close();
			fileStream.Close();
			return true;
		}

		private void OverallForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SaveDlgData();
		}

		private List<FormStructure> m_lstFormData = new List<FormStructure>();

		private List<BuiltInCategory> m_lstBuiltInCategory = new List<BuiltInCategory>();
	}
}
